/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.sys>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: property_parser.cpp
 *
 * Purpose: property parser implementation
 *
 * Developer:
 *   wen.gu , 2021-04-29
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "property_parser.h"
#include "utils.h"
#include "key_word.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

static bool CheckPropertyType(const DataTypeArray& type_defs, const YAML::Node& nd, std::string& out_type)
{
    if (!nd.IsScalar())
    {
        PrintError(nd, "the value of property type(key word: _type) must be a scalar ");
        return false;        
    }

    std::string type_str = nd.as<std::string>();

    if (!HasTypeDef(type_defs, type_str))
    {
        PrintTypeError(nd, "type ", type_str, "in invalid(e.g. not define before current line)");
        return false;
    }

    out_type = std::move(type_str);

    return true;
}

static bool CheckPropertyOptional(const YAML::Node& nd, bool& out_type)
{
    if (!nd.IsScalar())
    {
        PrintError(nd, "the value of property type(key word: _type) must be a scalar ");
        return false;        
    }   

    if (!NodeIsType<bool>(nd))
    {
        PrintError(nd, "the value of property optional(_setter, _getter or _notification) must be a boolean");
        return false;    
    }

    out_type = nd.as<bool>();

    return true;
}




static bool ParseProperty(const DataTypeArray& type_defs, PropertyInfo& fi, const YAML::Node& nd)
{
    if (!HasMember(nd, KW_TYPE))
    {
        PrintError(nd, "a property must have key word: _type ");
        return false;
    }
        
    if (!CheckPropertyType(type_defs, nd[KW_TYPE], fi.type))
    {
        return false;
    }

    fi.has_setter = false;
    fi.has_getter = false;
    fi.has_notification = false;
    

    if (HasMember(nd, KW_PROPERTY_SETTER) && (!CheckPropertyOptional(nd[KW_PROPERTY_SETTER], fi.has_setter)))
    {
        return false;
    }

    if (HasMember(nd, KW_PROPERTY_GETTER) && (!CheckPropertyOptional(nd[KW_PROPERTY_GETTER], fi.has_getter)))
    {
        return false;
    }

    if (HasMember(nd, KW_PROPERTY_NOTIFICATION) && (!CheckPropertyOptional(nd[KW_PROPERTY_NOTIFICATION], fi.has_notification)))
    {
        return false;
    }

    ParseComment(nd, fi.comment);

    return true;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

PropertyParser::PropertyParser()
    :ContentParser(ContentType::kProperty)
{
    /** todo something */
}

PropertyParser::~PropertyParser()
{
    /** todo something */
}

 
bool PropertyParser::parse(const YAML::Node& content_node, FileContent& file_content)
{
    if (!content_node.IsMap())
    {
        PrintError(content_node, "The property content node must be a object");
        return false;
    }

    PropertyArray& properties = file_content.properties;
    for (YAML::Node::const_iterator it = content_node.begin(); it != content_node.end(); ++it )
    {
        
        if (!it->first.IsScalar())
        {
            PrintError(it->first, "the name of property node must be scalar");
            return false;            
        }

        PropertyInfo fi;

        fi.name = it->first.as<std::string>();

        if (!it->second.IsMap())
        {
            PrintTypeError(it->first, "the property define node: ", fi.name, " must be a object");
            return false;            
        }

        if (!ParseProperty(file_content.type_defs, fi, it->second))
        {
            PrintTypeError(it->first, "parse property: ", fi.name, " failed");
            return false;
        }

        properties.push_back(std::move(fi));
    }

    return true;
}




